﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TiTi.tea.Comm;
using TiTi.tea.DAL;
using TiTi.tea.Model;

namespace TiTi.tea.BLL
{
    public class SpuBLL
    {
        /// <summary>
        /// 创建spu
        /// </summary>
        /// <param name="spu"></param>
        /// <param name="skus"></param>
        /// <returns></returns>
        public bool CreateSpu(Spu spu,List<Sku> skus)
        {
            SpuDAL spuDal = new SpuDAL();
            SkuDAL skuDal = new SkuDAL();

            spu.Status = 0;
            spu.CreateTime = DateTime.Now;
            spu.UpdateTime = DateTime.Now;
            spu.IsDelete = 0;


            spuDal.Add(spu,true);


            foreach (var item in skus)
            {
                item.SpuId = spu.Id;
                item.Status = 0;
                item.CreateTime = DateTime.Now;
                item.UpdateTime = DateTime.Now;
                item.IsDelete = 0;

                skuDal.Add(item, false);
            }

            return skuDal.SaveChanges();
        }

        /// <summary>
        /// 分页获取
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Spu> GetPage(string spuName,PagePara pagePara)
        {
            SpuDAL spuDal = new SpuDAL();
            if (string.IsNullOrEmpty(spuName)) spuName = "";

            return spuDal.GetAll(o=>o.SpuName.Contains(spuName)).SetPage(o=>o.CreateTime,pagePara);
        }

        /// <summary>
        /// 根据id获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Spu GetById(int id)
        {
            SpuDAL spuDal = new SpuDAL();
            return spuDal.GetOne(o => o.Id == id);
        }

        /// <summary>
        /// 修改基础信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public bool Update(Spu param)
        {
            SpuDAL spuDal = new SpuDAL();
            var spu = spuDal.GetOne(o=>o.Id== param.Id);
            spu.SpuName = param.SpuName;
            spu.Caption = param.Caption;
            spu.Price = param.Price;
            spu.CategoryId = param.CategoryId;
            spu.Introduction = param.Introduction;
            spu.IsDelete = param.IsDelete;
            spu.IsNew = param.IsNew;
            spu.IsHotSale = param.IsHotSale;
            spu.IsEnableMaterial = param.IsEnableMaterial;
            spu.IsEnableSpec = param.IsEnableSpec;

            return spuDal.Update(spu);
        }
    }
}
